package com.luzhikun.exer.dao;

import com.luzhikun.exer.bean.User;
import com.luzhikun.exer.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author Rookie Coder
 * @Description
 * @Date
 **/
public class UserDao {
    // 登录验证
    public User userLogin(String username,String password){
        User user=null;
        // 创建连接
        Connection connection=null;
        PreparedStatement pst=null;
        ResultSet rs=null;
        JDBCUtil jdbcUtil=new JDBCUtil();
        String sql="select * from t_user where username= ? and password = ?";

        try {
            connection= jdbcUtil.getConnection();
            pst=connection.prepareStatement(sql);
            pst.setString(1,username);
            pst.setString(2,password);
            rs = pst.executeQuery();
        if (rs!=null){
            while (rs.next()){

                user=new User();
                user.setUsername(rs.getString("username"));
                user.setPasswrod(rs.getString("password"));
                user.setAge(rs.getInt("age"));

                return user;
            }
        }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                jdbcUtil.closeAll(connection,pst,rs);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        return user;

    }


}
